﻿// -----------------------------------------------------------------------
// <copyright file="ApplicationsViewModel.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TouchExplorer.Entities
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using Microsoft.WindowsAPICodePack.Taskbar;
    using System.Collections.ObjectModel;
    using TouchExplorer.Controls;
    using System.Windows.Controls;
    using System.Windows.Forms;
    using System.Windows;
    using System.Drawing;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ApplicationsViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<RunningApp> Apps { get; set; }

        public ApplicationsViewModel()
        {
            Apps = new ObservableCollection<RunningApp>();
        }

        public void Load()
        {
            //Apps.Clear();
            //var runningapps = System.Diagnostics.Process.GetProcesses(".").Where(p => p.MainWindowTitle.Length > 0);
            //TaskbarManager.Instance.TabbedThumbnail.InvalidateThumbnails();
            //foreach (System.Diagnostics.Process p in runningapps)
            //{
            //    RunningApp app = new RunningApp(p);
            //    app.LoadImage();
            //    Apps.Add(app);
            //}

            this.InitializeThumbnails();
        }

        private void InitializeThumbnails()
        {
            IntPtr handle = ((System.Windows.Interop.HwndSource)System.Windows.Interop.HwndSource.FromVisual(System.Windows.Application.Current.Windows[0])).Handle;
            IntPtr current = WinAPI.GetWindow(handle, WinAPI.GetWindowCmd.First);

            do
            {
                int GWL_STYLE = -16;
                uint normalWnd = 0x10000000 | 0x00800000 | 0x00080000;
                uint popupWnd = 0x10000000 | 0x80000000 | 0x00080000;
                var windowLong = WinAPI.GetWindowLong(current, GWL_STYLE);
                var text = WinAPI.GetText(current);
                if (((normalWnd & windowLong) == normalWnd || (popupWnd & windowLong) == popupWnd) && !string.IsNullOrEmpty(text))
                {
                    var app = new RunningApp();
                    app.WndHnd = current;
                    app.Name = text;
                    app.Content = new Thumbnail();
                    app.Content.Width = 150;
                    app.Content.Height = 150;
                    app.Content.Source = current;
                    Apps.Add(app);
                }

                current = WinAPI.GetWindow(current, WinAPI.GetWindowCmd.Next);

                if (current == handle)
                    current = WinAPI.GetWindow(current, WinAPI.GetWindowCmd.Next);
            }
            while (current != IntPtr.Zero);
            //Opacity = 1;

            //invalidate layout, without this thumbnails will be invisible
            //Width++;
            //Width--;
        }

        protected void OnPropertyChanged(string p)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(p));
        }
    }
}
